// pages/settings/myinfo/myinfo.js
const app = getApp();
const globalData = getApp().globalData;
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const util = require('../../../utils/util.js');
const userservice = require('../../../apiservice/user/userservice');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		Fullname: '',
		Phone: '',
		Email: '',
		Hospital: '',
		Department: '',
		MobileroleSystemuser: '',
		UserType: '',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var userInfo = wx.getStorageSync("useridentity");

		this.setData({
			Fullname: userInfo.fullname ? userInfo.fullname : "",
			Phone: userInfo.mobilephone ? userInfo.mobilephone : "",
			Email: userInfo.emailAddress ? userInfo.emailAddress : "",
			Hospital: userInfo.site ? userInfo.site : "",
			Department: userInfo.businessunitname ? userInfo.businessunitname : "",
			MobileroleSystemuser: userInfo.mobilerole_Systemuser ? userInfo.mobilerole_Systemuser : "",
			UserType: userInfo.new_UserType ? userInfo.new_UserType : ""
		});
	},
	communitySuccess: function (res) {
		if(res.eventChannel)
		res.eventChannel.emit('openedCommunityPage', {
			data: 'detail'
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		if (typeof this.getTabBar === 'function' &&
			this.getTabBar()) {
			this.getTabBar().setData({
				selected: "setting"
			})
		}

		let userInfo = globalData.userInfo;
		let mobilerole = userInfo.mobilerole_systemuser;
		// if (userInfo.site == "爱尔眼科医院集团") { //集团
		//   let roleIds = [1, 2, 3];
		//   this.showTabBarItem(mobilerole, roleIds, "showScreens");

		//   roleIds = [5, 4, 7];
		//   this.showTabBarItem(mobilerole, roleIds, "showPromotion");
		// }
		// else {  //医院  医院配了筛查，则学术推广也显示
		//   let roleIds = [1, 2, 3];  //筛查
		//   if (util.haveRole(mobilerole, roleIds)) {
		//     if (typeof this.getTabBar === 'function' &&
		//       this.getTabBar()) {
		//       this.getTabBar().setData({
		//         ["showScreens"]: true,
		//         ["showPromotion"]: true
		//       })
		//     }
		//   }
		//   else { //医院没有配置筛查
		//     this.getTabBar().setData({
		//       ["showScreens"]: false
		//     })
		//     let roleIds = [5, 4, 7];
		//     this.showTabBarItem(mobilerole, roleIds, "showPromotion");
		//   }
		// }

		// let roleIds = [12, 13];
		// this.showTabBarItem(mobilerole, roleIds, "showFollow");
		let roleIds = [1, 2, 3];
		this.showTabBarItem(mobilerole, roleIds, "showScreens");
		roleIds = [5, 4, 7, 13]; //13: 村医
		this.showTabBarItem(mobilerole, roleIds, "showPromotion");
		roleIds = [12];
		this.showTabBarItem(mobilerole, roleIds, "showFollow");

	},

	showTabBarItem: function (mobilerole, roleIds, attrName) {
		let that = this;
		if (util.haveRole(mobilerole, roleIds)) {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: true
				})
			}
		} else {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: false
				})
			}
		}
	},

	exitClick: async function () {
		var sm = await this.showModalSync('提示', '您确定要执行注销操作？');
		if (sm.confirm) {
			// 调用注销了
			var result = await userservice.userlogOut("");
			wx.removeStorageSync("useridentity")
			wx.removeStorageSync("token")
			wx.redirectTo({
				url: '../../login/login',
			})
		} else if (sm.cancel) {
			console.log('用户点击取消')
		}

	},

	showModalSync: function (title, content) {
		return new Promise((resolve, reject) => {
			wx.showModal({
				title: title,
				content: content,
				success: function (sm) {
					resolve(sm);
				},
				fail: function (err) {
					reject(err);
				}
			})
		});
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
})
